import {err_code} from './api.js';
class Base{
	constructor(arg) {
	    // this.baseRequestUrl = Config.resUrl;
	}
	
	
	//跳转登录
	go_login(){
		let token = uni.getStorageSync('token');
		if(!token){
			uni.navigateTo({
			    url: '/pages/user/login'
			});
		}
	}
	
	//验证状态
	isVerifyCode(code){
		//必须登录
		if(code == err_code.isNotLogin){
			uni.setStorageSync('token',null);
			uni.showToast({
			    title: '请您先登录',
			    duration: 2000,
				success:function(){
					uni.navigateTo({
						url:"/pages/user/login"
					});
				}
			});
		}
	}
	
	
	

	// 请求体封装
	request(url, method = "GET", data = {},is_token = 0,is_loading = 1) {
		let that = this;
		if(is_loading == 1){
			wx.showLoading({
				title: "加载中..."
			});
		}
		let token = uni.getStorageSync('token');
		let header = is_token?{'content-type': 'application/json','token':token}:{'content-type': 'application/json'};
		data.token = token;
		return new Promise((resolve, reject) => {
			uni.request({
				url: url,
				data: data,
				method: method,
				header: header,
				success: function(res) {
					// if(res.data.code != 200){
						// console.log('错误码:' + res.data.code + ' - 详细: ' + res.data.msg + ' - 请求接口: ' + url)
					// }$ErrCode
					//验证状态
					that.isVerifyCode(res.data.code);
					// res.data.code == 200 ? resolve(res.data) : reject(res.data)
					res.data.code ? resolve(res.data) : reject(res.data)
					if(is_loading !== 0)uni.hideLoading();
				},
				fail: function(res) {
					uni.showToast({
						title: '网络错误',
					})
				}
			})
		})
	
	}
	
	
	
	
	//获取今天的日期
	getNowFormatDate() {
		var date = new Date();
		var seperator1 = "-";
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var strDate = date.getDate();
		if (month >= 1 && month <= 9) {
			month = "0" + month;
		}
		if (strDate >= 0 && strDate <= 9) {
			strDate = "0" + strDate;
		}
		var currentdate = year + seperator1 + month + seperator1 + strDate;
		return currentdate;
	}
	
	
	//跳转路由
	skip(url){
		uni.navigateTo({
		    url:url
		});
	}
	
	//获取多少天后的日期
	GetDateStr(AddDayCount = 10) {   
	   var dd = new Date();  
	   dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期  
	   var y = dd.getFullYear();   
	   var m = (dd.getMonth()+1)<10?"0"+(dd.getMonth()+1):(dd.getMonth()+1);//获取当前月份的日期，不足10补0  
	   var d = dd.getDate()<10?"0"+dd.getDate():dd.getDate();//获取当前几号，不足10补0  
	   // console.log(y+"-"+m+"-"+d)
	   return y+"-"+m+"-"+d;   
	};

	
	
	
	// 上传封装
	requestFile(url, data = [],is_token = 0) {
		wx.showLoading({
			title: "加载中..."
		});
		let token = uni.getStorageSync('token');
		let header = is_token?{'Accept': 'application/vnd.myapp.v1+json','token':token}:{'Accept': 'application/vnd.myapp.v1+json'}; 
		console.log(data[0])
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: url, //仅为示例，非真实的接口地址
				filePath: data[0],
				name: 'file',
				header: header,
				success: (res) => {
					if(res.data.status_code == 200 && res.data.status_code == 204){
						console.log('错误码:' + res.data.status_code + ' - 详细 : ' + res.data.message)
					}
					res.statusCode === 200 ? resolve(res.data) : reject(res.data)
					uni.hideLoading();
				},
				fail: function(res) {
					uni.showToast({
						title: '网络错误',
					})
				}
			});
		})
	
	}
	

	
	
	
	// //http请求
	// request(params,eCallback){
	// 	var that = this;
	// 	var url = Config.resUrl + params.url;
	// 	params.type = params.type?params.type:'GET';
	// 	uni.request({
	// 	    url: url,
	// 		data: params.data,
	// 		method:params.type,
	// 		header: {
	// 		  'content-type': 'application/json' ,// 默认值
	// 		  // 'content-type': 'application/json' ,// 默认值
	// 		},
	// 	    success: (res) => {
	// 			params.sCallback && params.sCallback(res.data);
	// 	    }
	// 	});
	// }
	
	
	
	


	
	
	
}
export {Base};